package net.sycu.guomy.ems.servlet.user;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sycu.guomy.ems.business.*;
import net.sycu.guomy.ems.entity.*;
import net.sycu.guomy.ems.servlet.BaseServlet;

public class UserMainServlet extends BaseServlet {
	NoteService noteService = new NoteService();
	MessageService messageService = new MessageService();
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		int loginId = this.getLogin().getLoginId();
		MessageSearcher searcher = new MessageSearcher();
		searcher.setDepartmentSearcher(new DepartmentSearcher());
		searcher.setPublisherSearcher(new LoginSearcher());
		searcher.setDepartmentId(this.getLogin().getDepartmentId());
		
		List<MessageBean> publicMessages = messageService.searchPublicMessageTop10();
		List<MessageBean> departmentMessages = messageService.searchDepartmentMessageTop10(searcher);

		request.setAttribute("messages1", publicMessages);
		request.setAttribute("messages2", departmentMessages);
		
		request.setAttribute("notes1", noteService.searchSendeeTop10(loginId));
		request.setAttribute("notes2", noteService.searchSenderTop10(loginId));

		this.getSession().removeAttribute("alertMessage");
		request.getRequestDispatcher("./userMain.jsp").forward(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		NoteBean note = null;
		LoginBean login = this.getLogin();
		note = noteService.search(this.getIntegerParameter("hidNoteId"));
		boolean isRead = this.getBooleanParameter("hidIsRead");
		if (note != null) {
			if (note.getSendeeId() == login.getLoginId()) {
				note.setRead(isRead);
				try {
					noteService.modify(note);
				} catch (Exception ex) {}
			}
		}
		doGet(request, response);
	}

}
